<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>模拟菜单树生成</title>
    <style>
        body { font-family: Arial; padding: 20px; }
        pre { background: #f5f5f5; padding: 10px; border-radius: 5px; }
        .success { color: green; font-weight: bold; }
        .error { color: red; font-weight: bold; }
    </style>
</head>
<body>
    <h1>模拟菜单树生成（不依赖框架）</h1>
    
    <?php
    $host = '113.44.136.17';
    $port = 3306;
    $database = 'qiandao';
    $username = 'qiandao';
    $password = 'NGbks3HWx5PhSxDX';
    
    try {
        $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        echo "<h2>1. 从数据库获取菜单</h2>";
        
        $stmt = $pdo->query("
            SELECT id, pid, title, icon, url, node, params, sort, status
            FROM system_menu
            WHERE status = 1
            ORDER BY sort DESC, id ASC
        ");
        
        $allMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo "<p>数据库中启用的菜单总数: " . count($allMenus) . "</p>";
        
        // 构建树形结构
        function buildTree($items, $pid = 0) {
            $tree = [];
            foreach ($items as $item) {
                if ($item['pid'] == $pid) {
                    $children = buildTree($items, $item['id']);
                    if (!empty($children)) {
                        $item['sub'] = $children;
                    }
                    $tree[] = $item;
                }
            }
            return $tree;
        }
        
        $tree = buildTree($allMenus, 0);
        
        echo "<h2>2. 树形结构（过滤前）</h2>";
        echo "<pre>";
        
        function printTree($menus, $level = 0) {
            $indent = str_repeat('  ', $level);
            foreach ($menus as $menu) {
                echo $indent . "├─ {$menu['title']} (ID:{$menu['id']}, URL:{$menu['url']}, NODE:{$menu['node']})\n";
                if (!empty($menu['sub'])) {
                    echo $indent . "│  [子菜单: " . count($menu['sub']) . " 个]\n";
                    printTree($menu['sub'], $level + 1);
                }
            }
        }
        
        printTree($tree);
        echo "</pre>";
        
        echo "<h2>3. 检查权限管理菜单</h2>";
        
        $authMenu = null;
        foreach ($tree as $menu) {
            if ($menu['id'] == 9) {
                $authMenu = $menu;
                break;
            }
        }
        
        if ($authMenu) {
            echo "<p class='success'>✓ 找到权限管理菜单</p>";
            echo "<pre>";
            echo "ID: {$authMenu['id']}\n";
            echo "标题: {$authMenu['title']}\n";
            echo "URL: {$authMenu['url']}\n";
            echo "NODE: {$authMenu['node']}\n";
            echo "二级菜单数量: " . (empty($authMenu['sub']) ? 0 : count($authMenu['sub'])) . "\n\n";
            
            if (!empty($authMenu['sub'])) {
                echo "二级菜单：\n";
                foreach ($authMenu['sub'] as $sub) {
                    echo "  ├─ {$sub['title']} (ID:{$sub['id']}, URL:{$sub['url']}, NODE:{$sub['node']})\n";
                    
                    if (!empty($sub['sub'])) {
                        echo "     [三级菜单: " . count($sub['sub']) . " 个]\n";
                        foreach ($sub['sub'] as $subsub) {
                            echo "     │  └─ {$subsub['title']} (ID:{$subsub['id']}, URL:{$subsub['url']}, NODE:{$subsub['node']})\n";
                        }
                    } else {
                        echo "     [无三级菜单]\n";
                    }
                }
            }
            echo "</pre>";
            
            // 判断显示位置
            $hasLevel3 = false;
            if (!empty($authMenu['sub'])) {
                foreach ($authMenu['sub'] as $sub) {
                    if (!empty($sub['sub'])) {
                        $hasLevel3 = true;
                        break;
                    }
                }
            }
            
            echo "<h3>判断结果：</h3>";
            if ($hasLevel3) {
                echo "<p class='success' style='font-size:20px'>✓ 权限管理有三级菜单，应该显示在【左侧菜单栏】</p>";
            } else {
                echo "<p class='error' style='font-size:20px'>❌ 权限管理只有二级菜单，会显示在【顶部菜单栏】</p>";
            }
            
        } else {
            echo "<p class='error'>❌ 没有找到权限管理菜单</p>";
        }
        
        echo "<h2>4. 所有一级菜单及其层级</h2>";
        echo "<table border='1' cellpadding='5' cellspacing='0'>";
        echo "<tr><th>菜单名称</th><th>二级菜单数</th><th>有三级菜单?</th><th>显示位置</th></tr>";
        
        foreach ($tree as $menu) {
            $level2Count = empty($menu['sub']) ? 0 : count($menu['sub']);
            
            $hasLevel3 = false;
            if (!empty($menu['sub'])) {
                foreach ($menu['sub'] as $sub) {
                    if (!empty($sub['sub'])) {
                        $hasLevel3 = true;
                        break;
                    }
                }
            }
            
            $position = $hasLevel3 ? '左侧' : '顶部';
            $color = $hasLevel3 ? 'green' : 'orange';
            
            echo "<tr>";
            echo "<td><strong>{$menu['title']}</strong></td>";
            echo "<td>{$level2Count}</td>";
            echo "<td>" . ($hasLevel3 ? '是' : '否') . "</td>";
            echo "<td style='color:{$color}'><strong>{$position}</strong></td>";
            echo "</tr>";
        }
        
        echo "</table>";
        
    } catch (PDOException $e) {
        echo "<p class='error'>数据库错误: " . $e->getMessage() . "</p>";
    }
    ?>
    
    <hr>
    <p><strong>说明：</strong></p>
    <ul>
        <li>有三级菜单的一级菜单 → 显示在左侧菜单栏</li>
        <li>只有二级菜单的一级菜单 → 显示在顶部菜单栏</li>
    </ul>
    
</body>
</html>

